﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Hosting;

namespace Pay.Core {
    public class CsvHelper {
        private CsvHelper() { }
        private static CsvHelper _instance;
        private static readonly object obj = new object();
        public static CsvHelper GetInstance() {
            if (_instance == null) {
                lock (obj) {
                    if (_instance == null) {
                        _instance = new CsvHelper();
                        return _instance;
                    }
                }
            }
            return _instance;
        }

        public Dictionary<string, string> GetHsCodeList() {
            //todo:先判断缓存是否有数据，有则取缓存，否则读文件。
            string path = CommonHelper.MapPath(string.Format("{0}/hsCode.csv", "~/bin/csv"));
            return ReadCsvFile(path);
        }

        public Dictionary<string, string> GetUnitList() {
            //todo:先判断缓存是否有数据，有则取缓存，否则读文件。
            string path = CommonHelper.MapPath(string.Format("{0}/unit.csv", "~/bin/csv"));
            return ReadCsvFile(path);
        }

        public Dictionary<string, string> GetAreaCodeList() {
            //todo:先判断缓存是否有数据，有则取缓存，否则读文件。
            string path = CommonHelper.MapPath(string.Format("{0}/area.csv", "~/bin/csv"));
            return ReadCsvFile(path);
        }

        ////public Dictionary<string, string> GetPackageCodeList() {
        ////    //todo:先判断缓存是否有数据，有则取缓存，否则读文件。
        ////    string path = CommonHelper.MapPath(string.Format("{0}/package.csv", RequestMethod.csv_path));
        ////    return ReadCsvFile(path);
        ////}

        ////public Dictionary<string, string> GetUnitCodeList() {
        ////    //todo:先判断缓存是否有数据，有则取缓存，否则读文件。
        ////    string path = CommonHelper.MapPath(string.Format("{0}/unit.csv", RequestMethod.csv_path));
        ////    return ReadCsvFile(path);
        ////}

        private Dictionary<string, string> ReadCsvFile(string path) {
            var dics = new Dictionary<string, string>();
            var rows = File.ReadLines(path);
            foreach (var row in rows) {
                var arrs = row.Split(',');
                if (string.IsNullOrEmpty(arrs[0]) || string.IsNullOrEmpty(arrs[1]))
                    continue;
                dics.Add(arrs[0], arrs[1]);
            }
            return dics;
        }
    }

    public class CommonHelper {
        /// <summary>
        /// Maps a virtual path to a physical disk path.
        /// </summary>
        /// <param name="path">The path to map. E.g. "~/bin"</param>
        /// <returns>The physical path. E.g. "c:\inetpub\wwwroot\bin"</returns>
        public static string MapPath(string path) {
            if (HostingEnvironment.IsHosted) {
                //hosted
                return HostingEnvironment.MapPath(path);
            }

            //not hosted. For example, run in unit tests
            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            path = path.Replace("~/", "").TrimStart('/').Replace('/', '\\');
            return Path.Combine(baseDirectory, path);
        }
    }

}
